import Dialog from './dialog';
import itemTpl from '../_tpl/recruit.ejs';
import scroller from './scroller';

const dialog = new Dialog('#dialog-recruit', {
    onOpen: () => {
        const myScroller = scroller('#dialog-recruit .recruit-scroller');
    }
});
const $contact = $('#section-contact');

$contact
    .on('click', '.open-recruit', () => {
        dialog.open();
    })
    .on('change', '.upload-intro', function() {
        const $el = $(this);
        const $next = $el.next();
        if ($next.text() === '处理中。。。') return;
        const t = $next.text();
        $next.text('处理中。。。');

        const data = new FormData($el.parents('form')[0]);
        // console.log(data);
        $.ajax({
            type: 'POST',
            url: '/c/Recruit/addMyDoc',
            processData: false,
            contentType: false,
            data: data,
            success: res => {
                alert(res);
                dialog.close();
            },
            error: xhr => {
                if (err.status === 413) {
                    alert('提交失败，文件不能超过60M！')
                    return;
                }
                alert(xhr.responseText);
            },
            complete: () => {
                $el.val('');
                $next.text(t);
            }
        });
    });

getData();

function getData() {
    $.ajax({
        url: '/c/Recruit/lists',
        success: res => {
            let html = '';
            res.list.forEach(item => {
                html += itemTpl(item);
            });
            $contact.find('.jobs').html(html);
        }
    });
}
